import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import hilog from '@ohos.hilog';
import UIAbility from '@ohos.app.ability.UIAbility';
import Want from '@ohos.app.ability.Want';
import window from '@ohos.window';
import { GlobalThis } from '../Utils/GlobalThis';
import { BusinessError } from '@ohos.base';

//import featureAbility from '@ohos.ability.featureAbility';

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    GlobalThis.getInstance().setContext('context', this.context)
  }

  onDestroy(): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage): void {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });


    //2024年3月14日注释全屏显示 正式运行时再开启
    windowStage.getMainWindow().then((data) => {
      data.setFullScreen(true, (err, data) => {
        if (err.code) {
          console.error('Failed to enable the full-screen mode. Cause:' + JSON.stringify(err));
          return;
        }
        console.info('Succeeded in enabling the full-screen mode. Data: ' + JSON.stringify(data));
      });

      //设置沉浸式全屏
       /*data.setWindowLayoutFullScreen(true, (err, data) => {
         if (err.code) {
           console.error('Failed to enable the full-screen mode. Cause:' + JSON.stringify(err));
           return;
         }
         console.info('Succeeded in enabling the full-screen mode. Data: ' + JSON.stringify(data));
       });
       //设置顶部与底部栏不显示
       let names: Array<'status' | 'navigation'> = [];
       try {
         let promise = data.setWindowSystemBarEnable(names);
         promise.then(() => {
           console.info('Succeeded in setting the system bar to be invisible.');
         }).catch((err: BusinessError) => {
           console.error('Failed to set the system bar to be invisible. Cause:' + JSON.stringify(err));
         });
       } catch (exception) {
         console.error('Failed to set the system bar to be invisible. Cause:' + JSON.stringify(exception));
       }
       //禁止触屏
        try {
          data.setWindowTouchable(false, (err: BusinessError) => {
            const errCode: number = err.code;
            if (errCode) {
              console.error('Failed to set the window to be touchable. Cause:' + JSON.stringify(err));
              return;
            }
            console.info('Succeeded in setting the window to be touchable.');
          });
        } catch (exception) {
          console.error('Failed to set the window to be touchable. Cause:' + JSON.stringify(exception));
        }*/
    })


  }

  onWindowStageDestroy(): void {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground(): void {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground(): void {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
};
